package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.impl.GoodsDaoImpl;
import com.qf.entity.Goods;
import com.qf.entity.GoodsType;
import com.qf.entity.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeService;

import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.service.impl
 * @description: ${TODO}
 * @date 2022/6/23 11:39
 */
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao=new GoodsDaoImpl();
    @Override
    public PageBean<Goods> findByPage(int pn, int ps, String where, List<Object> params) {
        long totalSize=goodsDao.selectCount(where,params);
        List<Goods> data=goodsDao.selectByPage(pn,ps,where,params);
        PageBean<Goods> pageBean=new PageBean<>(pn, ps, totalSize, data);
        return pageBean;
    }

    @Override
    public Goods findById(int id) {
        Goods goods=goodsDao.selectById(id);
        if(goods==null){
            throw new RuntimeException("没有此商品");
        }
        //根据商品类别id查询
        GoodsTypeService goodsTypeService=new GoodsTypeServiceImpl();
        GoodsType goodsType=goodsTypeService.findById(goods.getTypeid());
        goods.setGoodsType(goodsType);
        return goods;
    }

    @Override
    public void add(Goods goods) {
        goodsDao.insert(goods);
    }
}
